package com.infore.siteManagement.repository;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.infore.siteManagement.entity.Project;

import java.lang.String;
/**
 * 因子/指标仓库
 * @author 刘乐法
 *
 *
 */
@Repository
public interface IndexRespository extends JpaRepository<Project, String>,JpaSpecificationExecutor<Project>{
	
	@Modifying
	@Transactional
	int deleteByProjectIdIn(List<String> ids);
	
	@Query(value = "select t.project_id from PROJECT t where t.project_code = :code" ,nativeQuery = true)
	String findIdByCode(@Param("code") String code);
	
	@EntityGraph(value = "project_view")
	List<Project> findByProjectCodeIn(List<String> projectcode);

	@Query(value = "select t.* from PROJECT t " ,nativeQuery = true)
	List<Map<String, Object>> findAllInfo();
	
	/**通过id查询因子**/
	@EntityGraph(value = "project_view")
	List<Project> findByProjectIdIn(List<String> ids);
	
	/**查询站点下因子**/
	@Query(value="select * from project p where p.project_code "
			+ "in (select distinct( t.project_code) "
			+ "from PROJECT_AND_STATION t where t.station_code in (:mns) ) order by p.SORT_INDEX asc",nativeQuery=true)
	List<Project> queryAllByMns(@Param("mns")List<String> mns);
	
	/**通过站点id查询因子信息**/
	@Query(value="select * from project t " + 
			"where t.project_code in (" + 
			"select distinct(ps.project_code) from project_and_station ps " + 
			"where ps.station_code in (select s.station_mn from station s where s.station_id in (:stationIds)) )",nativeQuery=true)
	List<Project> queryBySatationId(@Param("stationIds") List<String>stationIds);
	
	List<Project> findByProjectName(String name);
}
